import PostForm from '@/components/post/post-form'
import { getPostBySlug } from '@/db/queries'
import { auth } from '@/lib/auth'
import { headers } from 'next/headers'
import { notFound, redirect } from 'next/navigation'
import React from 'react'

export default async function EditPostPage({ params }: { params: Promise<{ slug: string }> }) {

    const { slug } = await params
    const session = await auth.api.getSession({
        headers: await headers()
    })

    if (!session || !session.user) {
        redirect('/')
    }

    const post = await getPostBySlug(slug)
    if (!post) {
        notFound()
    }

    if (post.authorId !== session.user.id) {
        redirect('/')
    }

    return <main className='py-10 max-w-4xl mx-auto'>
        <h1 className='font-bold mb-6 text-4xl'>Edit Post</h1>
        <PostForm isEditing={true} post={post} />
    </main>
}
